package com.结构型模式.享元模式;

import javax.swing.*;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;


/**
 * 抽象享元角色
 * 棋子
 */
interface ChessPieces{
    /**
     * 下子
     * @param g
     * @param p
     */
    void downPieces(Graphics g,Point p);
}

/**
 * 具体享元角色
 * 白子
 */
class WhitePrices implements  ChessPieces{
    @Override
    public void downPieces(Graphics g, Point p) {
        g.setColor(Color.white);
        g.fillOval(p.x,p.y,30,30);
    }
}

/**
 * 具体享元角色
 * 黑子
 */
class BlackPrices implements  ChessPieces{
    @Override
    public void downPieces(Graphics g, Point p) {
        g.setColor(Color.black);
        g.fillOval(p.x,p.y,30,30);
    }
}

/**
 * 享元工厂
 * 五子棋
 */
class WuZiQiFactory{
    private ArrayList<ChessPieces> qz ;
    WuZiQiFactory(){
        qz = new ArrayList<ChessPieces>();
        ChessPieces w = new WhitePrices();
        qz.add(w);
        ChessPieces b = new BlackPrices();
        qz.add(b);
    }
    ChessPieces getChessPrices(String type){
        if(type.equalsIgnoreCase("w")){
            return (ChessPieces)qz.get(0);
        }
        else if(type.equalsIgnoreCase("b")){
            return (ChessPieces)qz.get(1);
        }
        else{
        return null;
        }
    }

}

/**
 * 客户角色
 * 棋盘
 *
 */
class ChessBoard extends MouseAdapter {
    WuZiQiFactory wf;
    JFrame f;
    Graphics g;
    JRadioButton wz;
    JRadioButton bz;
    private final int x = 50;
    private final int y = 50;
    private final int w = 40; //方格宽与高
    private final int rw = 400; //棋盘宽与高



    ChessBoard(){
        wf = new WuZiQiFactory();
        f = new JFrame("五子棋");
        f.setBounds(100,100,500,500);
        f.setVisible(true);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel southJP = new JPanel();
        f.add("South",southJP);
        wz = new JRadioButton("白子");
        bz = new JRadioButton("黑子",true);
        ButtonGroup group = new ButtonGroup();
        group.add(wz);
        group.add(bz);
        southJP.add(wz);
        southJP.add(bz);
        JPanel centerJP = new JPanel();
        centerJP.setLayout(null);
        centerJP.setSize(500,500);
        centerJP.addMouseListener(this);
        f.add("Center",centerJP);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        g = centerJP.getGraphics();
        g.setColor(Color.blue);
        g.drawRect(x,y,rw,rw);
        for(int i = 1;i<10;i++){
            //绘制第i条竖直线
            g.drawLine(x+(i*w),y,x+(i*w),y+rw);
            //绘制第i条水平线
            g.drawLine(x,y+(i*w),x+rw,y+(i*w));

        }


    }
    @Override
    public void mouseClicked(MouseEvent e) {
      Point pt = new Point(e.getX()-15,e.getY()-15);
      if(wz.isSelected()){
          ChessPieces c1 = wf.getChessPrices("w");
          c1.downPieces(g,pt);
      }
      else if(bz.isSelected()){
          ChessPieces c2 = wf.getChessPrices("b");
          c2.downPieces(g,pt);
      }
    }

}
/**
 * 享元模式
 * 五子棋
 *
 */
public class Flyweight {
    public static void main(String[] args) {
        new ChessBoard();
    }



}
